/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clipper.tray;

import clipper.clipboard.ClipBored;
import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/**
 *
 * @author mahadevan
 */
public class ClipTray {

    private TrayIcon trayIcon = null;
    private MenuItem[] textItems = new MenuItem[50];
    private int textCount = 0;
    private int selectedTextIndex = -1;
    private MenuItem defaultItem;
    private  PopupMenu popup;
    private MenuItem aboutItem;
    private ActionListener menuListener;
    private ClipBored clipbored;
    private String[] allClippedTexts;

    public ClipTray() {

        if (SystemTray.isSupported()) {

            //Get an instance of clipbored.
            clipbored = ClipBored.getClipBored();

            SystemTray sysTray = SystemTray.getSystemTray();

            Image image = null;

            InputStream inStream = getClass().getResourceAsStream("img/attach.gif");

            try {
                image = ImageIO.read(inStream);
            } catch (IOException ex) {
                Logger.getLogger(ClipTray.class.getName()).log(Level.SEVERE, null, ex);
            }

            //Create a MenuListener
            menuListener = new ActionListener() {

                public void actionPerformed(ActionEvent e) {

                    for (int i = 0; i < textCount; i++) {
                        if (e.getSource().equals(textItems[i])) {
                            selectedTextIndex = i;
                            clipbored.writeToClipBoard(allClippedTexts[selectedTextIndex]);
                            break;
                        }
                    }
                }
            };


            //Create an exitListener
            ActionListener exitListener = new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    if (e.getSource().equals(defaultItem)) {
                        System.exit(0);
                    }
                }
            };


            //Create an aboutListener
            ActionListener aboutListener = new ActionListener() {

                public void actionPerformed(ActionEvent e) {

                    if (e.getSource().equals(aboutItem)) {
                        trayIcon.displayMessage("Clipbored!", "By Mahadevan.S - http://www.twitter.com/karmic_learner", TrayIcon.MessageType.INFO);
                    }
                }

            };

            //create PopupMenu
            popup = new PopupMenu();

            //create ExitItem
            defaultItem = new MenuItem("Exit");
            defaultItem.addActionListener(exitListener);

            //create AboutItem
            aboutItem = new MenuItem("About Clipboard!");
            aboutItem.addActionListener(aboutListener);

            //Add ExitItem and AboutItem to PopupMenu
            popup.add(defaultItem);
            popup.add(aboutItem);

            //create Trayicon
            trayIcon = new TrayIcon(image, "Clipbored!", popup);

            //trayIcon.setImageAutoSize(true);

            try {
                //add trayicon to systemtray
                sysTray.add(trayIcon);
            } catch (AWTException ex) {
                System.err.println();

            }


  
        }
    }


    public int getSelectedTextIndex() {
        return selectedTextIndex;
        
    }


    public void addTextToMenu(String clippedText, String[] allClippedTexts) {

        this.allClippedTexts = allClippedTexts;


        if (clippedText.length() > 30) {
            clippedText = clippedText.substring(0,30) + "...";
        }

        MenuItem tempItem = new MenuItem("temp");

        textItems[textCount] = tempItem;
        textItems[textCount].setLabel(clippedText);
        textItems[textCount].addActionListener(menuListener);
        popup.add(textItems[textCount++]);

    }
}
